package com.oanda.blackberry.view;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;

public class TestField extends LabelField {
    private String label = "";
    private String time;
    private int fontColorFocused = 255;
    private int fontColorUnfocused = 65535;
    private int backgroundColorFocused = 8421504;
    private int backgroundColorUnfocused = 11119017;
    private int textColorFocused = 16776960;
    private int textColorUnfocused = 32768;
    int horizontalPadding = 2;
    int verticalPadding = 2;
    Timer t;
    TimerTask tt = new TimerTask() {
        public void run() {
            int i = Calendar.getInstance().get(13);
            // TODO
            // TestField.access$002(TestField.this, String.valueOf(i));
            TestField.this.invalidate();
        }
    };

    public TestField() {
        this.t = new Timer();
        this.t.schedule(this.tt, 0L, 1000L);
    }

    protected void layout(int maxWidth, int maxHeight) {
        Font font = getFont();
        int width = font.getAdvance(this.label + "99") + this.horizontalPadding * 2;
        int height = font.getHeight() + this.verticalPadding * 2;

        setExtent(Math.min(width, maxWidth), Math.min(height, maxHeight));
    }

    protected void paint(Graphics graphics) {
        graphics.setColor(isFocus() ? this.backgroundColorFocused : this.backgroundColorUnfocused);
        graphics.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);

        graphics.setColor(isFocus() ? this.textColorFocused : this.textColorUnfocused);
        graphics.drawRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
        int fontWidth = getFont().getAdvance(this.label + " " + this.time);
        graphics.drawText(this.label + " " + this.time, (getWidth() - fontWidth) / 2,
                this.verticalPadding);
    }

    public boolean isFocusable() {
        return true;
    }

    protected void drawFocus(Graphics graphics, boolean on) {
    }

    protected void onFocus(int direction) {
        super.onFocus(direction);
        invalidate();
    }

    protected void onUnfocus() {
        super.onUnfocus();
        invalidate();
    }
}
